<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" >
    <title>攻击型技能</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" href="static/layui/css/layui.css"  media="all" charset="utf-8">
    <script src="static/jquery-3.5.1/jquery-3.5.1.js"></script>
    <script src="static/layui/layui.js" charset="utf-8"></script>

    <style>

        .layui-table-cell{
            height:auto;/*设置表格行高*/
            /*white-space: normal;*//*可以使文字自适应*/

        }
        .layui-form-item .layui-input-inline {


        }

    </style>
</head>
<body>

<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-pane" style="margin-top: 15px;">
        <label class="layui-form-label">查询条件</label>

        <div class="layui-input-inline">
            <input type="text" name="name"  placeholder="技能名称" autocomplete="off" class="layui-input">
        </div>

        </div>
        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block" >
                <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" id="doSearch">查询</button>
                <button type="reset" class="layui-btn layui-btn-warm  layui-icon layui-icon-refresh">重置</button>
            </div>
        </div>
    </div>
</form>

<!-- 搜索条件结束 -->



<!-- 数据表格开始 -->
<div style="display: none;" id="tableToolBar">
    <button type="button" class="layui-btn layui-btn-sm" lay-event="add">增加</button>
    <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="deleteBatch">批量删除</button>
</div>
<div  id="trToolbar" style="display: none;height:auto">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</div>
<!-- 数据表格结束 -->


<!-- 数据表格开始 -->
<table class="layui-hide" id="myTable" lay-filter="myTable"></table>
<!-- 数据表格结束 -->



<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form"  lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id"  id="id">
                    <input type="text" name="name" lay-verify="required"   placeholder="名称" autocomplete="off"
                           class="layui-input">
                </div><br>
                <label class="layui-form-label">冷却时间</label>
                <div class="layui-input-block">
                    <input type="number" name="cd" lay-verify="required"   placeholder="秒数" autocomplete="off"
                           class="layui-input">
                </div><br>
                <label class="layui-form-label">伤害值</label>
                <div class="layui-input-block">
                    <input type="number" name="atk" lay-verify="required"   placeholder="伤害值" autocomplete="off"
                           class="layui-input">
                </div><br>
                <label class="layui-form-label">技能描述</label>
                <div class="layui-input-block">

                    <textarea name="description" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>

    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<script type="text/javascript"  >
    var tableIns;
    layui.use([ 'jquery', 'layer', 'form', 'table'  ], function() {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;

        //渲染数据表格
        tableIns=table.render({
            elem: '#myTable'   //渲染的目标对象: 就是表格的 id
            ,url:'/atkSkill/selectByPage' //后台地址
            ,title: '攻击型技能数据'//数据导出来的标题
             ,toolbar:"#tableToolBar"   //表格的工具条
            //,height:'full-220'
            ,cellMinWidth:50 //设置列的最小默认宽度
            ,page: true  //是否启用分页
            ,cols: [[   //列表数据
                {type:'checkbox'}
                ,{field:'id', title:'ID',align:'center',width:'100'}
                ,{field:'name', title:'名称',align:'center',width:'100'}
                ,{field:'cd', title:'冷却时间',align:'center',width:'100'}
                ,{field:'atk', title:'伤害值',align:'center',width:'100'}
                ,{field:'description', title:'技能描述',align:'center',width:'340'}
                ,{field:'img', title:'技能图片',align:'center',width:'100',templet:function (d) {

                        var img = d.img;
                        return '<img style="width: auto;height: 40px" src='+img+'>'

                    }}
                ,{fixed: 'right', title:'操作', toolbar: '#trToolbar', width:180,align:'center'}
            ]]
        });

        //
        $('#doSearch').click(function () {

            //1.表单的数据
            var data =$('#searchFrm').serialize();
            //重新加载
            tableIns.reload({
                url:'/atkSkill/selectByPage?'+data,
                page:{
                    curr:1
                }
            });
        });


        //监听的表格：
        table.on('toolbar(myTable)',function (obj) {
             var event = obj.event;

             if(event=='add') {
                add();
             }
             if(event=='deleteBatch') {
                 deleteBatch();
             }

        });


        // 新增或更新的弹出层
        var layerDiv;
        var url ; //新增或者更新的提交地址
        function  add() {

            layerDiv = layer.open(
                {
                    type:1,
                    title:'添加攻击型技能',
                    content:$("#saveOrUpdateDiv"),
                    area:['400px','450px'],
                    success:function(index){
                        //清空表单数据
                        $("#dataFrm")[0].reset();
                        url="/atkSkill/add";
                    }
                }
            );
        }

        //批删除
        //批量删除
        function deleteBatch(){
            //得到选中的数据行
            var checkStatus = table.checkStatus('myTable');
            var data = checkStatus.data;
            var params="";
            $.each(data,function(i,item){
                if(i==0){
                    params+="ids="+item.id;
                }else{
                    params+="&ids="+item.id;
                }
            });
            layer.confirm('确认删除选中的这些技能吗', function(index){
                //向服务端发送删除指令
                $.post("/atkSkill/deleteBatch",params,function(res){
                    layer.msg(res.msg);
                    //刷新数据 表格
                    tableIns.reload();
                },'json')
            });
        }


        //保存或者更新的提交
        form.on('submit(doSubmit)',function (obj) {
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(rs){
                layer.msg(rs.msg);
                //关闭弹出层
                layer.close(layerDiv)
                //刷新数据 表格
                tableIns.reload();
            },'json')
        });


        //监听行工具事件
        table.on('tool(myTable)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除【'+data.name+'】吗', function(index){
                    //向服务端发送删除指令
                    $.post("/atkSkill/deleteById/"+data.id,function(res){
                        layer.msg(JSON.parse(res).msg);
                        //刷新数据 表格
                        tableIns.reload();
                    })
                });
            } else if(layEvent === 'edit'){ //编辑
                update(data);
            }
        });

        function  update(data) {
            layerDiv=layer.open({
                type:1,
                title:'修改技能',
                content:$("#saveOrUpdateDiv"),
                area:['400px','450px'],
                success:function(index){
                    form.val("dataFrm",data);
                    url="/atkSkill/updateById";
                }
            });
        }









    })
</script>



</body>
</html>